package com.lovo.pz.util;

import com.lovo.pz.role.*;
import com.lovo.pz.window.GamePanel;

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.List;

/**
 * 游戏主面板的事件处理类
 * @author 白德柯
 *
 */
public class GameControl implements MouseListener, MouseMotionListener {
    /** 游戏主面板 */
    private GamePanel gamePanel;

    public GameControl(GamePanel gamePanel){
        this.gamePanel = gamePanel;
    }
    @Override
    public void mouseClicked(MouseEvent e) {
        // 获取鼠标点击的坐标
        int x = e.getX();
        int y = e.getY();

        // 将鼠标的x和y坐标映射成草地上对应格子的坐标
        Point plantPoint = GameUtil.computerPosition(x,y);

        PlantRole selectedPlant = gamePanel.getSelectedPlant();
        // 当点击了鼠标后被选中的植物消失
        if(selectedPlant != null){
            gamePanel.setSelectedPlant(null);
        }
        PlantRole tempPlant = gamePanel.getTempPlant();
        if(tempPlant != null){
            // 判断当前鼠标下是否已经有植物，如果没有植物则安放植物，否则不安放
            for(int i = 0;i < gamePanel.getPlantRoleList().size();i++){
                if(gamePanel.getPlantRoleList().get(i).getRoleX() == plantPoint.x
                    && gamePanel.getPlantRoleList().get(i).getRoleY() == plantPoint.y){
                    gamePanel.setTempPlant(null);
                }
            }
            if(gamePanel.getTempPlant() != null){
                // 将植物安放在草地上
                gamePanel.getPlantRoleList().add(tempPlant);
                // 扣去当前植物所需的种子数
                gamePanel.getPlantPanelRole().setSeedCnt(gamePanel.getPlantPanelRole().getSeedCnt() - tempPlant.getCost());
                tempPlant.setRunStatus(BaseRole.SEND_BULLET);
                gamePanel.setTempPlant(null);
            }
        }

        // 判断鼠标是否点击到了某个植物卡片
        List<PlantCardRole> plantCardRoleList = gamePanel.getPlantCardRoleList();
        for(int i = 0;i < plantCardRoleList.size();i++){
            // 判断鼠标是否进入某个卡片的范围
            if(GameUtil.checkRoleRect(x,y,plantCardRoleList.get(i))){
                // 如果卡片为不可用状态则无法点击
                if(plantCardRoleList.get(i).getRunStatus() == BaseRole.CARD_DISABLED){
                    break;
                }

                if(plantCardRoleList.get(i).getPlantType() == PlantCardRole.PEA_SHOOTER){
                    PeaShooterRole peaShooterRole = new PeaShooterRole(gamePanel);
                    peaShooterRole.setRoleX(x - 25);
                    peaShooterRole.setRoleY(y - 35);
                    gamePanel.setSelectedPlant(peaShooterRole);
                }else if(plantCardRoleList.get(i).getPlantType() == PlantCardRole.SUN_FLOWER){
                    SunFlowerRole sunFlowerRole = new SunFlowerRole(gamePanel);
                    sunFlowerRole.setRoleX(x - 35);
                    sunFlowerRole.setRoleY(y - 35);
                    gamePanel.setSelectedPlant(sunFlowerRole);
                }
            }
        }

        // 判断鼠标是否点击到了某个太阳种子
        List<SunSeedRole> sunSeedRoleList = gamePanel.getSunSeedRoleList();
        for(int i = 0;i < sunSeedRoleList.size();i++){
            if(GameUtil.checkRoleRect(x,y,sunSeedRoleList.get(i))){
                gamePanel.getPlantPanelRole().setSeedCnt(gamePanel.getPlantPanelRole().getSeedCnt() + 25);
                sunSeedRoleList.get(i).setRunStatus(BaseRole.ROLE_END);
            }
        }

        // 判断铲子是否铲到了植物
        if(gamePanel.getShovelRole().getRunStatus() == BaseRole.SHOVEL_SELECTED){
            for(int i = 0;i < gamePanel.getPlantRoleList().size();i++){
                if(GameUtil.checkRoleRect(x,y,gamePanel.getPlantRoleList().get(i))){
                    gamePanel.getPlantRoleList().get(i).setRunStatus(BaseRole.ROLE_END);
                }
            }
        }

        // 先把铲子设置到初始位置
        gamePanel.getShovelRole().setRunStatus(BaseRole.NORMAL);
        gamePanel.getShovelRole().setRoleX(575);
        gamePanel.getShovelRole().setRoleY(0);
        // 判断鼠标是否点击铲子
        if(GameUtil.checkRoleRect(x,y,gamePanel.getShovelRole())){
            gamePanel.getShovelRole().setRunStatus(BaseRole.SHOVEL_SELECTED);
            gamePanel.getShovelRole().setRoleX(x - 40);
            gamePanel.getShovelRole().setRoleY(y - 40);
        }
    }

    @Override
    public void mouseMoved(MouseEvent e) {
        int x = e.getX();
        int y = e.getY();

        PlantRole selectedPlant = gamePanel.getSelectedPlant();
        // 得到植物在草地上阴影的坐标
        Point plantPoint = GameUtil.computerPosition(x,y);
        // 让被选中的植物跟随鼠标移动
        if(selectedPlant != null){
            selectedPlant.setRoleX(x - 25);
            selectedPlant.setRoleY(y - 35);
            // 如果鼠标进入草地，则plantPoint不为空
            if(plantPoint != null){
                if(selectedPlant.getPlantType() == PlantCardRole.PEA_SHOOTER){
                    PeaShooterRole peaShooterRole = new PeaShooterRole(gamePanel);
                    peaShooterRole.setRoleX(plantPoint.x);
                    peaShooterRole.setRoleY(plantPoint.y);
                    peaShooterRole.setRunStatus(BaseRole.PLANT_TEMP);
                    gamePanel.setTempPlant(peaShooterRole);
                }else if(selectedPlant.getPlantType() == PlantCardRole.SUN_FLOWER){
                    SunFlowerRole sunFlowerRole = new SunFlowerRole(gamePanel);
                    sunFlowerRole.setRoleX(plantPoint.x);
                    sunFlowerRole.setRoleY(plantPoint.y);
                    sunFlowerRole.setRunStatus(BaseRole.PLANT_TEMP);
                    gamePanel.setTempPlant(sunFlowerRole);
                }
            }else{
               // 鼠标移出草地，阴影对象设置为空
               gamePanel.setTempPlant(null);
            }
        }

        // 如果鼠标点击了铲子，则需要跟随鼠标移动
        if(gamePanel.getShovelRole().getRunStatus() == BaseRole.SHOVEL_SELECTED){
            gamePanel.getShovelRole().setRoleX(x - 40);
            gamePanel.getShovelRole().setRoleY(y - 40);
        }
    }

    @Override
    public void mousePressed(MouseEvent e) {

    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }

    @Override
    public void mouseDragged(MouseEvent e) {

    }

}
